package com.bishe.tax.service;

import com.bishe.tax.entity.SysUser;
import com.bishe.tax.entity.Taxpayer;
import com.bishe.tax.mapper.TaxpayerMapper;
import com.bishe.tax.utils.Convert;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 纳税人 服务层实现
 *
 * @author ruoyi
 * @date 2019-06-23
 */
@Service
public class TaxpayerService {
    protected final Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TaxpayerMapper taxpayerMapper;
    @Autowired
    IdentityService identityservice;
    @Autowired
    RuntimeService runtimeservice;
    @Autowired
    org.activiti.engine.TaskService taskservice;

    /**
     * 查询纳税人信息
     *
     * @param taxpayerId 纳税人ID
     * @return 纳税人信息
     */

    public Taxpayer selectTaxpayerById(Integer taxpayerId) {
        return taxpayerMapper.selectTaxpayerById(taxpayerId);
    }

    /**
     * 查询纳税人列表
     *
     * @param taxpayer 纳税人信息
     * @return 纳税人集合
     */

    public List<Taxpayer> selectTaxpayerList(Taxpayer taxpayer) {
        return taxpayerMapper.selectTaxpayerList(taxpayer);
    }

    /**
     * 新增纳税人
     *
     * @param taxpayer 纳税人信息
     * @return 结果
     */

    public int insertTaxpayer(SysUser user, Taxpayer taxpayer) {
        return taxpayerMapper.insertTaxpayer(taxpayer);

    }

    /**
     * 修改纳税人
     *
     * @param taxpayer 纳税人信息
     * @return 结果
     */

    public int updateTaxpayer(Taxpayer taxpayer) {
        return taxpayerMapper.updateTaxpayer(taxpayer);
    }

    /**
     * 删除纳税人对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */

    public void deleteTaxpayerByIds(String ids) {
        String array[] = Convert.toStrArray(ids);
        Taxpayer taxpayer = null;
        for (String id : array) {
            try {
                taxpayer = taxpayerMapper.selectTaxpayerById(Integer.valueOf(id));
                if (taxpayer != null && StringUtils.isNotBlank(taxpayer.getProcessInstanceId())) {
                    runtimeservice.deleteProcessInstance(taxpayer.getProcessInstanceId(), "");
                }
            } catch (Exception e) {
                log.error("", e);
            }
        }
        taxpayerMapper.deleteTaxpayerByIds(array);
    }

    public int doSubmitFlow(SysUser user, String txId) {
        Taxpayer taxpayer = taxpayerMapper.selectTaxpayerById(Integer.valueOf(txId));
        Map<String, Object> variables = new HashMap<>();
        String creator = user.getUserId().toString();
        variables.put("creator", creator);
        variables.put("type", "taxpayer");
        identityservice.setAuthenticatedUserId(user.getUserId().toString());
        ProcessInstance instance = runtimeservice.startProcessInstanceByKey("singleExamine",
                taxpayer.getTaxpayerId().toString(), variables);
        taxpayer.setProcessInstanceId(instance.getId());
        taxpayer.setStatus(2);
        return taxpayerMapper.updateTaxpayer(taxpayer);
    }

    /**
     * 查询纳税人列表
     *
     * @param taxpayer 纳税人信息
     * @return 纳税人集合
     */
    public List<Taxpayer> selectTaxpayerListByChoose(Taxpayer taxpayer) {
        return taxpayerMapper.selectTaxpayerListByChoose(taxpayer);
    }

    public List<Taxpayer> selectTaxpayerByRiId(Integer riId) {
        return taxpayerMapper.selectTaxpayerByRiId(riId);
    }

}
